﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;
using System.ComponentModel;
using WCYCReader.Pages;
using System.Windows.Input;

namespace WCYCReader.UserControls
{
    public partial class PopupContainerControl : UserControl
    {

        private Popup _Popup;

        private bool popupLoad;

        public PhoneApplicationPage _BasePage;

        private CubicEase _EasingFunction = new CubicEase()
        {
            EasingMode = EasingMode.EaseInOut
        };

        private FrameworkElement _PopupContentBottom
        {
            get;
            set;
        }

        private FrameworkElement _PopupContentPanel
        {
            get;
            set;
        }

        private FrameworkElement _PopupContentTop
        {
            get;
            set;
        }

        public bool IsShown
        {
            get
            {
                return this._Popup.IsOpen;
            }
        }

        public PopupContainerControl()
        {
            this.InitializeComponent();
            base.Loaded += this.PopupCotainer_Loaded;
        }

        private void BasePage_BackKeyPress(object sender, CancelEventArgs e)
        {
            this.ClosePanel();
            this.CloseTopBottom();
            e.Cancel = true;
        }

        public void ClosePanel()
        {
            this.PrepareCloseStoryPanel().Begin();
        }

        public void ClosePopupPanel()
        {
            this._BasePage.BackKeyPress -= this.BasePage_BackKeyPress;
            BookReaderPage bookReaderPage = this._BasePage as BookReaderPage;
            bookReaderPage.BottomTextVisible();
            bookReaderPage.BackKeyPress += bookReaderPage.OnBackKeyPress;
            Popup parent = base.Parent as Popup;
            if (parent != null)
            {
                parent.IsOpen = false;
            }
        }

        private void ClosePopupTopBottom()
        {
        }

        public void CloseTopBottom()
        {
            Storyboard storyboard = this.PrepareCloseStoryTop();
            Storyboard storyboard1 = this.PrepareCloseStoryBottom();
            storyboard.Begin();
            storyboard1.Begin();
        }

        
        private void mask_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.ClosePanel();
            this.CloseTopBottom();
        }

        private void PopupCotainer_Loaded(object sender, RoutedEventArgs e)
        {
            Storyboard storyboard = this.PrepareShowStoryTop();
            Storyboard storyboard1 = this.PrepareShowStoryBottom();
            storyboard.Begin();
            storyboard1.Begin();
        }

        private Storyboard PrepareCloseStoryBottom()
        {
            Storyboard storyboard = new Storyboard();
            storyboard.Completed += this.StoryReverse_Completed;
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(this.popupTransformBottom.TranslateY),
                To = new double?(800),
                Duration = new Duration(TimeSpan.FromMilliseconds(250)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformBottom);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        private Storyboard PrepareCloseStoryPanel()
        {
            Storyboard storyboard = new Storyboard();
            storyboard.Completed += this.StoryReverse_CompletedPanel;
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(this.popupTransformPanel.TranslateY),
                To = new double?(800),
                Duration = new Duration(TimeSpan.FromMilliseconds(400)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformPanel);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        private Storyboard PrepareCloseStoryTop()
        {
            Storyboard storyboard = new Storyboard();
            storyboard.Completed += this.StoryReverse_Completed;
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(this.popupTransformTop.TranslateY),
                To = new double?(0 - this.popupAreaTop.ActualHeight),
                Duration = new Duration(TimeSpan.FromMilliseconds(250)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformTop);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        private Storyboard PrepareShowStoryBottom()
        {
            if (this.contentAreaBottom.Children.IndexOf(this._PopupContentBottom) < 0)
            {
                this.contentAreaBottom.Children.Add(this._PopupContentBottom);
            }
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(800),
                To = new double?(0),
                Duration = new Duration(TimeSpan.FromMilliseconds(250)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformBottom);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        public Storyboard PrepareShowStoryPanel()
        {
            if (this.contentAreaPanel.Children.IndexOf(this._PopupContentPanel) < 0)
            {
                this.contentAreaPanel.Children.Add(this._PopupContentPanel);
            }
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(800),
                To = new double?(0),
                Duration = new Duration(TimeSpan.FromMilliseconds(250)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformPanel);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        private Storyboard PrepareShowStoryTop()
        {
            base.UpdateLayout();
            if (this.contentAreaTop.Children.IndexOf(this._PopupContentTop) < 0)
            {
                this.contentAreaTop.Children.Add(this._PopupContentTop);
            }
            this.contentAreaTop.UpdateLayout();
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                From = new double?(-80),
                To = new double?(0),
                Duration = new Duration(TimeSpan.FromMilliseconds(250)),
                EasingFunction = this._EasingFunction
            };
            Storyboard.SetTarget(doubleAnimation, this.popupTransformTop);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("TranslateY", new object[0]));
            storyboard.Children.Add(doubleAnimation);
            return storyboard;
        }

        public void ShowPanel(FrameworkElement popupContentPanel)
        {
            if (this.contentAreaPanel.Children.IndexOf(this._PopupContentPanel) >= 0)
            {
                this.contentAreaPanel.Children.Remove(this._PopupContentPanel);
            }
            this._PopupContentPanel = popupContentPanel;
            this._Popup.Child = this;
            this.PrepareShowStoryPanel().Begin();
        }

        public void ShowTopBottom(PhoneApplicationPage basePage, FrameworkElement popupContentTop, FrameworkElement popupContentBottom)
        {
            if (!this.popupLoad)
            {
                this._BasePage = basePage;
                this._Popup = new Popup()
                {
                    Child = this
                };
                this.popupLoad = true;
            }
            if (this.contentAreaTop.Children.IndexOf(this._PopupContentTop) >= 0)
            {
                this.contentAreaTop.Children.Remove(this._PopupContentTop);
            }
            if (this.contentAreaBottom.Children.IndexOf(this._PopupContentBottom) >= 0)
            {
                this.contentAreaBottom.Children.Remove(this._PopupContentBottom);
            }
            this._PopupContentBottom = popupContentBottom;
            this._PopupContentTop = popupContentTop;
            
            BookReaderPage bookReaderPage = this._BasePage as BookReaderPage;
            bookReaderPage.BackKeyPress -= bookReaderPage.OnBackKeyPress;
            this._BasePage.BackKeyPress += this.BasePage_BackKeyPress;
            (this._PopupContentTop as ReaderPageTopControl).UpdateIcon();

            
            
            //
            this._Popup.IsOpen = true;
        }

        private void StoryReverse_Completed(object sender, EventArgs e)
        {
            this.ClosePopupTopBottom();
        }

        private void StoryReverse_CompletedPanel(object sender, EventArgs e)
        {
            this.ClosePopupPanel();
        }
    }
}
